//
//  LHVisitorView.swift
//  weibao
//
//  Created by 李浩 on 16/4/5.
//  Copyright © 2016年 李浩. All rights reserved.
//

import UIKit

import SnapKit

import AFNetworking


protocol LHVisitorViewDelegate :NSObjectProtocol {

    //登陆方法

    func didLoginMthod()
    
    //注册方法

    func didRegistorView()
    

}



class LHVisitorView: UIView {

   
    
    /*
    
    自定义视图的三部曲 
    
    1.重写构造方法
    
    2.添加子视图和设置约束
    
    3.赖加载
    
    
    */
    
    //设置代理
    
    weak var delegate : LHVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        SetUpUI()
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func SetUpUI(){
    
        
      
        
        
        
            addSubview(BigImagView)
        
            addSubview(maskImageView)
       
        
      
        
        addSubview(iconImageView)
        
        
        addSubview(introLabel)
        
        addSubview(registerBtn)
        
        addSubview(landingBtn)
        
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX .equalTo( self.snp_centerX)
            
            make.centerY .equalTo( self.snp_centerY).offset(-10)
            
            
        }
        
        
        
        
       iconImageView.snp_makeConstraints { (make) -> Void in
        
        make.centerX.equalTo(self.snp_centerX)
        
        make.centerY.equalTo(self.snp_centerY)
        
        
        }
        
        
        BigImagView.snp_makeConstraints { (make) -> Void in
            
            
            make.centerY.equalTo(self.snp_centerY)
            
            make.centerX.equalTo(self.snp_centerX)
            
        }
        
        
        introLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(BigImagView.snp_bottom).offset(10)
            
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
            
            
            
        }
        
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(introLabel.snp_left)
            
            make.top.equalTo(introLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
            
            
            
        }
        
        
        landingBtn.snp_makeConstraints { (make) -> Void in
            
            
            make.right.equalTo(introLabel.snp_right)
            
            make.top.equalTo(introLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
            
            
            
            
        }
        
   
        
        
    
    
    }
    
  private  func StarAnimation (){
    
        //核心动画 -- keypath 如果写错了就没有效果
        
        
        let baseAnimation = CABasicAnimation(keyPath: "transform.rotation")
    
        //时间
    
        baseAnimation.duration = 10
    
        //转动的角度
        
        baseAnimation.toValue = M_PI
        
        //重复次数
        
        baseAnimation.repeatCount = MAXFLOAT
        

        //当我们切换页面的时候,动画会停止
        
        baseAnimation.removedOnCompletion = false
        
        
        BigImagView.layer.addAnimation(baseAnimation, forKey: nil)
        
        
        
    
    }
    
    
    
    //定义一个函数 修改显示图片和文字
    
    func setUpInfo(image : String?,message:String){
     
      
        if let img = image{
        
       iconImageView.image = UIImage(named:img)
            
        BigImagView.hidden=true
            
        
        }else{
        
           BigImagView.hidden = false
        
            StarAnimation()
        
        }
        
        
        introLabel.text = message
        
        
    }
    
    
    
    
    
    
   
    //赖加载控件
    
    //添加遮罩
    
    
    lazy var maskImageView :UIImageView = {
    
        let img = UIImageView()
    
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        
        return img
    
    
    }()
    
    
    
    
    lazy var iconImageView :UIImageView = {
    
        
        let img = UIImageView()
        
        img.image=UIImage(named: "visitordiscover_feed_image_house")
    
    
       return img
    
    
    }()
    
    
    
    lazy var BigImagView :UIImageView = {
    
    
        let img = UIImageView()
    
        img.image=UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    
    
    }()
    
    lazy var introLabel :UILabel = {
        
        let introlab = UILabel()
        
        introlab.text = "关注一些人，回这里看看有什么惊喜"
        
        
        introlab.textColor=UIColor.darkGrayColor()
        
        introlab.textAlignment = NSTextAlignment.Center
        
        introlab.numberOfLines = 0
        
        introlab.font=UIFont.systemFontOfSize(14)
        
        return introlab
        
        
       }()
    
    
    lazy var registerBtn :UIButton = {
        
        let Btn = UIButton ()
        
        
        Btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        
        Btn.setTitle("注册", forState: UIControlState.Normal)
        
        
        Btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        
        Btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return Btn
        
        }()
    
    
    lazy var landingBtn :UIButton = {
    
       let btn = UIButton()
    
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn .addTarget(self, action: "landingBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
    
        return btn
    
    }()
    
    
    func registerBtnClick(){
    
        
        
        
        delegate?.didRegistorView()
        
        
        
    
    
    }
    
    
    func landingBtnClick(){
    
    
        
        
        
        delegate?.didLoginMthod()
        
    
    
    }
    
    
    
    
    
    
    
    
    
    

}
